package com.yuanda.wordhot.repository;

import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yuanda.wordhot.entity.ConfigEntity;

@Repository
public class ConfigRepository extends HibernateGenericDAO<ConfigEntity> implements IRepository<ConfigEntity>{
	@SuppressWarnings("unchecked")
	public List<ConfigEntity> queryConfigsByType(String type) {
		return sessionFactory.getCurrentSession()
				.createCriteria(this.persistClass)
				.add(Restrictions.eq("type", type))
				.add(Restrictions.eq("valid", true))
				.addOrder(Order.asc("order")).list();
	}

	@SuppressWarnings("unchecked")
	public List<ConfigEntity> queryConfigsByParent(String parentId) {
		return sessionFactory.getCurrentSession()
				.createCriteria(this.persistClass)
				.add(Restrictions.eq("parentId", parentId))
				.add(Restrictions.eq("valid", true))
				.addOrder(Order.asc("order")).list();
	}

	/**
	 * 查询子节点的数量
	 */
	public long queryChildSize(String configId) {
		String s = new String();
		return (Long) sessionFactory.getCurrentSession()
				.createCriteria(ConfigEntity.class)
				.add(Restrictions.eq("parentId", configId))
				.setProjection(Projections.rowCount()).uniqueResult();
	}
}
